layui.use(['admin', 'form'], function () {
    let $ = layui.$
        , setter = layui.setter
        , admin = layui.admin
        , router = layui.router()
        , laytpl = layui.laytpl
        , view = layui.view
        , form = layui.form;

    const path = router.path; // 获取路由链接
    const pathURL = admin.correctRouter(path.join('/')); // 转换链接格式
    const dataName = setter.response.dataName; // 数据详情的字段名称
    const $body = $('body');
    // 获取菜单
    admin.req({
        url: setter.request.domainName + '/admin/Index/index' //实际使用请改成服务端真实接口
        , data: {}
        ,type: 'POST'
        ,loading: false // 是否开启加载框
        , done: function (res) {
            let menuList = res[dataName].menus;
            let authObj  = res[dataName].authList;
            let authList = [];
            if(typeof authObj  === 'object'){
                layui.each(authObj, function(index, item){
                    authList[index] = item;
                });
            }
            layui.data(setter.tableName, {
                key: setter.authList
                , value: authList
            });

            layui.data(setter.tableName, {
                key: setter.cdnUrl
                , value: res.data.siteConfig.cdn_url
            });
            let siteName = res[dataName].siteConfig.site_name;
            $("#siteName").html(siteName);
            $("#siteTitle").html(siteName);
            let html     = '';
            layui.each(menuList, function(index, item){
                if(item.type === 'button'){
                    return false;
                }
                let hasChildren = typeof item.children === 'object' && item.children.length > 0;
                if(hasChildren && item.children[0]['type'] === 'button'){
                    hasChildren = false;
                }
                let classSelected = function(){
                    const match = path[0] === item.path || ( (index === 0 || index === '0') && !path[0]) || (item.path && pathURL === admin.correctRouter(item.path)) || item.spread;
                    if(match){
                        return hasChildren ? 'layui-nav-itemed' : 'layui-this';
                    }
                    return '';
                };
                let url     = (item.path && typeof item.path === 'string') ? item.path : item.name;
                let name    = item.name || ''; // 规则名称
                let jump    = item.path || ''; // 路由路径
                let hrefStr = hasChildren ? '':"lay-href='"+ url +"'"; // 判断是否有子菜单
                let title   = item.title; // 标题
                let icon    = item.icon;
                html += '<li data-name="'+name+'" data-jump="'+jump+'" class="layui-nav-item '+classSelected()+'">';
                html += '   <a href="javascript:;" '+hrefStr+' lay-tips="'+title+'" lay-direction="2">';
                html += '       <i class="iconfont '+icon+'"></i>';
                html += '       <cite>'+title+'</cite>';
                html += '   </a>';
                if(hasChildren){
                    html += bindHTML(item.children);
                }
                html += '</li>';
            })
            $body.find("#LAY-system-side-menu").html(html);
            layui.element.render('nav', 'layadmin-system-side-menu');
        }
    });

    let bindHTML = function bindHTML(result){
        let html = '<dl class="layui-nav-child">';
        result.forEach((item, index) => {
            if(item.type === 'button'){
                return false;
            }
            const jumpURL = admin.correctRouter(item.path); // 转换链接格式
            let hasChildren = typeof item.children == 'object' && item.children.length > 0;
            if(hasChildren && item.children[0]['type'] === 'button'){
                hasChildren = false;
            }
            let classSelected2 = function(){
                let match = (item.path && pathURL.indexOf(jumpURL) !== -1 && hasChildren) || (item.path && pathURL === jumpURL && !hasChildren) || item.spread;
                // console.log(pathURL , jumpURL);
                if(match){
                    return hasChildren ? 'layui-nav-itemed' : 'layui-this';
                }
                return '';
            }
            let name2    = item.name || ''; // 规则名称
            let jump2    = item.path || ''; // 路由路径
            let url2 = (item.path && typeof item.path === 'string') ? item.path : '';
            let hrefStr2 = hasChildren ? '':"lay-href='"+ url2 +"'"; // 判断是否有子菜单
            let title2   = item.title; // 标题
            let icon    = item.icon;
            let title   = item.title; // 标题
            html += '   <dd data-name="'+name2+'" data-jump="'+jump2+'" class=" '+classSelected2()+'">';
            html += '       <a href="javascript:;" '+hrefStr2+' lay-tips="'+title+'"><i class="iconfont '+icon+'"></i>'+title2+'</a>';
            if(hasChildren){
                html += bindHTML(item.children);
            }
            html += '   </dd>';
        })
        html += '</dl>';
        return html;
    }
    // 退出登录
    admin.events.logout = function(){
        layui.common.logout();
    }
    // 基本资料
    admin.events.basicInfo = function(){
        let popupId = "admin-popup-basic-info";
        admin.popup({
            title: '基本资料'
            , area: ['750px', '500px']
            , id: popupId
            , success: function (layero, index) {
                let layId = this.id;
                let field = {};
                // 获取角色信息
                admin.req({
                    url: setter.request.domainName + '/admin/auth.admin/basicInfo'
                    , data: field
                    , type: 'get'
                    , loading: false // 是否开启加载框
                    , done: function (res) {
                        let initData = res.data.row;
                        initData['lay_index'] = index;
                        initData['popupId'] = popupId;
                        view(layId).render('auth/admin/basicInfo', initData).done(function () {
                            form.render(null, 'layBasicInfoPopup');
                        });
                    }
                })
            }
        });
    }
    // 修改密码
    admin.events.changePassword = function(){
        let popupId = "admin-popup-change-password";
        admin.popup({
            title: '修改密码'
            , area: ['750px', '500px']
            , id: popupId
            , success: function (layero, index) {
                let layId = this.id;
                let initData = [];
                initData['lay_index'] = index;
                initData['popupId'] = popupId;
                view(layId).render('auth/admin/changePassword', initData).done(function () {
                    form.render(null, 'layChangePassPopup');
                });
            }
        });
    }

})
